//axios
import axios from 'axios'
import {ElMessage} from "element-plus";
import 'element-plus/theme-chalk/el-message.css'
import {useUserstore} from "@/stores/user.js";

const http = axios.create({
  //基地址
  baseURL: 'http://pcapi-xiaotuxian-front-devtest.itheima.net',
  //超时时间
  timeout: 5000
})
//拦截器
// axios请求拦截器
http.interceptors.request.use(config => {
  //获取token数据
  const useStore = useUserstore()
  //拼接token数据
  const token = useStore.userInfo.token
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
}, e => Promise.reject(e))

// axios响应式拦截器
http.interceptors.response.use(res => res.data, e => {
  //统一错误提示
  ElMessage({
    message: e.response.data.message,
    type: 'warning'
  })
  return Promise.reject(e)
})

export default http

